package com.gbits.app.finance.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gbits.app.finance.domain.DeptSubjectMapper;
import com.gbits.app.finance.service.DefaultAbstractService;
import com.gbits.app.finance.service.DeptSubjectMapperService;

@Service("deptSubjectMapperService")
@Transactional
public class DeptSubjectMapperServiceImpl extends
		DefaultAbstractService<DeptSubjectMapper> implements
		DeptSubjectMapperService {

	public DeptSubjectMapper getDeptSubject(String costOrgName, String orgCode) {
		
		String hql = "from " + DeptSubjectMapper.class.getName() + " e where e.deptName=:deptName and e.orgCode=:orgCode";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgCode", orgCode);
		map.put("deptName", costOrgName);
		List<DeptSubjectMapper> list = this.getBaseDao().findByNamesParameter(hql, map);
		if (null == list || list.isEmpty()) {
			throw new RuntimeException(String.format("未配制成本中心[%s-%s]的部门核算", orgCode, costOrgName));
		}
		
		if (list.size() > 1) {
			throw new RuntimeException(String.format("成本中心[%s-%s]的部门核算不止一条", orgCode, costOrgName));
		}
		return list.get(0);
	}

	public DeptSubjectMapper getProductSubject(String costOrgName,
			String orgCode) {
		String hql = "from " + DeptSubjectMapper.class.getName() + " e where e.deptName=:deptName and e.orgCode=:orgCode";
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orgCode", orgCode);
		map.put("deptName", costOrgName);
		List<DeptSubjectMapper> list = this.getBaseDao().findByNamesParameter(hql, map);
		if (null == list || list.isEmpty()) {
			throw new RuntimeException(String.format("未配制成本中心[%s-%s]的产品核算", orgCode, costOrgName));
		}
		
		if (list.size() > 1) {
			throw new RuntimeException(String.format("成本中心[%s-%s]的产品核算不止一条", orgCode, costOrgName));
		}
		return list.get(0);
	}

}
